//
//  NSString+URLEncode.m
//  category
//
//  Created by MAC on 2018/5/12.
//  Copyright © 2018年 MAC. All rights reserved.
//

#import "NSString+URLEncode.h"

@implementation NSString (URLEncode)

- (NSString *)ex_urlEncode {
    NSString *characterString = @"!*'\"();:@&=+$,/?%#[]%";
    NSCharacterSet *character = [NSCharacterSet characterSetWithCharactersInString:characterString];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:character];
}

- (NSString *)ex_urlDecode {
    return [self stringByRemovingPercentEncoding];
}

- (NSDictionary *)ex_dictionaryFromURLParameters
{
    NSArray *pairs = [self componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *pair in pairs) {
        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val = [[kv objectAtIndex:1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [params setObject:val forKey:[kv objectAtIndex:0]];
    }
    return params;
}
@end
